#include "process.h"
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#define NUM 101
#define STYLE '='
#define POINT '.'
#define SPACE ' '
const int pnum = 6;

// version1
//void Process()
//{
//    const char *lable = "|/-\\";
//    int len = strlen(lable);
//    char bar[NUM];
//    int cnt = 0;
//    memset(bar,'\0',sizeof(bar));
//    while(cnt <= 100)
//    {
//        bar[cnt] = STYLE;
//        printf("[%-100s][%d%%][%c]\r",bar,cnt,lable[cnt%len]);
//        fflush(stdout);
//        cnt++;
//        usleep(50000);
//    }
//
//    printf("\r\n");
//}
//


// version2  真实的进度条，应该根据具体的下载量，来动态刷新进度

//void Process(double total,double current)
void FlushProcess(double total,double current)
{
    double rate = (current/total)*100;
   // printf("test:%.1f%%\r",rate);
   // fflush(stdout);

    // 2.更新进度条主体
    char bar[NUM];
    memset(bar,'\0',sizeof(bar));
    //for(int i = 0;i < (int)rate;i+=2)
    for(int i = 0;i < (int)rate;i++)
    {
        bar[i] = STYLE;
    }

    // 更新旋转光标或者其他风格
    static int num = 0;
    num++;
    num %= pnum;

    char points[pnum+1];
    memset(points,'\0',sizeof(points));
    for(int i = 0;i < pnum;i++)
    {
        if(i<num)  points[i] = POINT;
        else points[i] = SPACE;
    }

    printf("[%-100s][%.1lf%%]%s\r",bar,rate,points);
    fflush(stdout);
}


